package com.itcoon.cloud.framework.webmvc.utils;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.itcoon.cloud.framework.utils.SpringUtils;
import com.itcoon.cloud.framework.webmvc.response.AbstractResponse;
import org.springframework.http.MediaType;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;

/**
 * @author Administrator
 * created in 2021/4/14
 */
public class ApiResponseUtils {

    private static final ObjectMapper objectMapper = SpringUtils.getBean(ObjectMapper.class);

    public static <T> void writeJsonResponse(HttpServletResponse response, AbstractResponse<T> body, Integer status) {
        response.setStatus(status == null ? 200 : status);
        response.setContentType(MediaType.APPLICATION_JSON_VALUE);
        response.setCharacterEncoding(StandardCharsets.UTF_8.name());
        try {
            PrintWriter writer = response.getWriter();
            writer.write(objectMapper.writeValueAsString(body));
            writer.flush();
        }catch (IOException e){
            throw new RuntimeException(e.getMessage());
        }
    }

    public static <T> void writeJsonResponse(AbstractResponse<T> body, Integer status) {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
        HttpServletResponse response = requestAttributes.getResponse();
        response.setStatus(status == null ? 200 : status);
        response.setContentType(MediaType.APPLICATION_JSON_VALUE);
        response.setCharacterEncoding(StandardCharsets.UTF_8.name());
        try {
            PrintWriter writer = response.getWriter();
            writer.write(objectMapper.writeValueAsString(body));
            writer.flush();
        }catch (IOException e){
            throw new RuntimeException(e.getMessage());
        }
    }

    public static <T> void writeJsonResponse(HttpServletResponse response, T body, Integer status) {
        response.setStatus(status == null ? 200 : status);
        response.setContentType(MediaType.APPLICATION_JSON_VALUE);
        response.setCharacterEncoding(StandardCharsets.UTF_8.name());
        try {
            PrintWriter writer = response.getWriter();
            writer.write(objectMapper.writeValueAsString(body));
            writer.flush();
        } catch (IOException e) {
            throw new RuntimeException(e.getMessage());
        }
    }

    public static <T> void writeJsonResponse(T body, Integer status) {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletResponse response = requestAttributes.getResponse();
        response.setStatus(status == null ? 200 : status);
        response.setContentType(MediaType.APPLICATION_JSON_VALUE);
        response.setCharacterEncoding(StandardCharsets.UTF_8.name());
        try {
            PrintWriter writer = response.getWriter();
            writer.write(objectMapper.writeValueAsString(body));
            writer.flush();
        } catch (IOException e) {
            throw new RuntimeException(e.getMessage());
        }
    }
}
